package algorithm.middle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: letterCombinations17
 * @description: 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * @date 2020/8/2617:18
 * @since V1.0
 */
public class letterCombinations17 {
    public static void main(String[] args) {
        letterCombinations("23");
    }

    public static List<String> letterCombinations(String digits) {
        Map<Character,String> map = new HashMap<>();
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        StringBuilder str = new StringBuilder();
        ArrayList<String> list = new ArrayList<String>();
        int index = 0;
        circle(map,str,digits,index,list);
        System.out.println(list);
        return null;
    }

    public static void circle(Map<Character,String>map, StringBuilder str, String digits,int index, ArrayList<String> list ){
        if(index == digits.length()){
            list.add(str.toString());
        }else{
            for(int m=0;m< map.get(digits.charAt(index)).length();m++ ){
                str.append(map.get(digits.charAt(index)).charAt(m));
                circle(map,str,digits,index+1,list);
                str.deleteCharAt(index);
            }
        }
    }
}
